<?php
namespace Yzadmin\Controller;
use Think\Controller;
/**
* 文章管理
*/
class NewsController extends CkController{
    

    //文章列表首页
    public function index()
    {   
        $p=I('get.p')?(int)I('get.p'):0;//当前页数
        $cid=I('get.cid')?(int)I('get.cid'):'';
        $num=10; //每页调用条数
        //$where=array("News.look"=>0,); //调用没有锁定文章
        if($cid){$where["News.cid"]=$cid;} //判断有没有选择分类id
        $newslist=D("NewsView"); //实例化视图模型
        $count=$newslist->where($where)->count();//计算总数
        $data['newslist']=D("NewsView")->where($where)->order("News.hot desc,News.sort asc,News.id desc")->page($p.','.$num)->select();
        
        $Page= new \Think\Page($count,$num);// 实例化分页类 传入总记录数和每页显示的记录数
        $Page->setConfig('prev','上一页');
        $Page->setConfig('next','下一页');
        $data["cate"]=M("cate")->select();  //调用分类
        $data['page']= $Page->show();// 分页显示输出
        $data['title']="新闻列表";
        $this->assign($data);
        $this->display();
    }
     

    //文章添加
    public function add()
    {   
        if(IS_POST){
           $url= I('post.url');
           if($url){
             $data['news']=$this->cai($url);
             $data['news']=$data['news'];
           }
        }
        $data['cate']=M("cate")->where("look=0")->select(); //得到所有分类
        $data['shop_item']=M("shop_item")->field("id,title")->select();  //调用商品
        $data['yumi']=M("yumi")->where(array("look"=>0,'type'=>1))->order("Rand()")->select(); //得到所有部署域名iD这里采集了一个随机排序
        $data['title']="文章添加";
        $this->assign($data);
        $this->display();
    } 
    
    
    
    //采集处理
    private function cai($url)
    {
       import("Lib.QueryList");
       $q= new \QueryList();
       $reg = array(
      //抓取网站标题
       "title" => array("title","text"),
       "content" => array("#js_content","html")
       );
       $data=$q->Query($url,$reg);
       preg_match_all('/msg_cdn_url = "(.*?)"/',$data->html, $pic);
       $data=$data->jsonArr;
       $data=$data[0];
       $data['litpic']=C("WEIXIN_PIC_URL").$pic['1']['0'];
      // $data['content']=strtr($data['content'],"src='".C("WEIXIN_PIC_URL"), "data-src='");
      
       $data['content']=@ereg_replace('data-src=', 'src=', $data['content']);
       $data['content']=@ereg_replace('width: auto !important;', '', $data['content']);
       $data['content']=@ereg_replace('http://', C("WEIXIN_PIC_URL")."http://", $data['content']);
       return $data;

    } 
   
   
   //匹配图片
   private function  get_img($str)
   {
      $str = stripslashes($str);

      $pattern="/^http(s)?:\\/\\/.+/";
      preg_match_all($pattern, $str, $matches);
      return $matches[2]; 
   }


    
    //文章添加和更新处理全部在这里处理
    //判断标题如果重复就更新,如果不存在就重新添加
    public function add_update()
    {
        $data=I('post.');
        $data=self::datas($data);
        $news=D("News");
        //判断是不是系统标题的文章，如果有就更新文章,如果Id或者有相同文章标题都更新文章
        $_news=$news->where(array('title'=>$data['title']))->find();
        if(!$news->create($data)){ 
        $this->error($news->getError());
        die();
        }
        if(!empty($_news) || $data['id'])
        {
           //这里为更新文章 
           $id=get_content_id($_news,$data['id']);
           if($news->where(array("id"=>$id))->save($data)!==false)
           {
             $this->success("文章更新成功!",U("News/index"));
           }
        }else{ 
           //这里为添加文章
           if($news->add($data)!==false)
           {
            $this->success("文章添加成功!",U("News/index"));
           }
        }
    }
    

    //修改文章
    public function update()
    {
        $id=I('get.id');
        $data['news']=M("News")->find($id);
        $data['cate']=M("cate")->where("look=0")->select(); //得到所有分类
        $data['shop_item']=M("shop_item")->field("id,title")->select();  //调用商品
        $data['yumi']=M("yumi")->where(array("look"=>0,'type'=>1))->order("Rand()")->select(); //得到所有部署域名iD这里采集了一个随机排序
        $data['title']="文章修改";
        $this->assign($data);
        $this->display();
    }
    

    //文章删除
    public function dels()
    {
      $id= I("get.id");
      if(!$id){ $this->error("请选择要删除的文章!");}
      if(M("News")->where("id=".$id)->delete()!==false)
      {
        $this->success("删除成功!",U("News/index"));
      }else{
        $this->error("删除失败!",U("News/index"));
      }
    }
    
    //文章批量删除
    public function p_del()
    {
       $ids =I('post.id');
      if(!$ids){ $this->error("请选择要删除的文章!");}
      if(M("News")->where(array('id'=>array('in',$ids)))->delete()!==false)
      {
        $this->success("批量删除成功!",U("News/index"));
      }else{
        $this->error("批量删除失败!",U("News/index"));
      }
    }


    //域名管理
    public function yumi()
    {
       $data['yumi_list']=M("yumi")->order("id asc")->select();
       $data['news_count']=M("News")->count();
       $data['title']="域名管理";
       $this->assign($data);
       $this->display();
    }


    //添加域名
    public function add_yumi()
    {  
       $data['title']="添加域名";
       $this->assign($data);
       $this->display();
    }
    
   
   //域名状态修改AJax
   public function yumi_update_ajax()
   {
      $data=I('post.');
      if(IS_AJAX){ 
         if(M("yumi")->where(array('id'=>$data['id']))->save($data)!==false){ 
            $this->ajaxReturn(array('error'=>0,"mag"=>'状态更新成功!'));
            die();
         }
      }else{ 
            $this->ajaxReturn(array('error'=>0,"mag"=>'非法访问!'));
      }
   }
   

   //域名修改处理
   public function yumi_updates()
   {
      $data=I('post.');
      if(empty($data['look'])){ 
         $data['look']=1;    
      }else{ 
         $data['look']=0;
      }
      if(empty($data['url'])){ 
         $this->error("请输入域名!");
      }
      if(M('yumi')->where(array('id'=>$data['id']))->save($data)!==false){ 
         $this->success("域名修改成功!");
      }else{
         $this->error("域名修改失败!请检查!");
      }
   }


   //域名修改
   public function yumi_update()
   {  
      $id=I('get.id');
      $data['yumi']=M('yumi')->find($id);
      $data['title']="域名修改";
      $this->assign($data);
      $this->display();
   }


   //域名添加处理
   public function yumi_adds()
   {
      $data=I('post.');
      if(empty($data['look'])){ 
         $data['look']=1;    
      }else{ 
         $data['look']=0;
      }
      if(empty($data['url'])){ 
         $this->error("请输入域名!");
      }
      if(M('yumi')->add($data)!==false){ 
         $this->success("域名添加成功!");
      }else{
         $this->error("添加失败!请检查!");
      }
   }


    //对是有内容数据进行处理
    private function datas($data)
    {  
       $data['time']=date('Y-m-d');
       if(empty($data['yumi_id'])){ 
         $yumi=M("yumi")->where("look=0")->field("id")->order("Rand()")->find();
         $data['yumi_id']=$yumi['id'];
       }
       if(empty($data['click'])){
         $data['click']=rand(4019,19918);
       } 
       return $data;
    }


    //下载远程图片
    private function get_yc_pic($content)
    {   
        echo "<img src='";
        echo 'http://img01.store.sogou.com/net/a/04/link?appid=100520033&w=320&url=http://mmbiz.qpic.cn/mmbiz/kCQUzqM7zOpyxBCdvYlEks4lsmVxDLApyVt0GTvcibME7QFJTGLVwhFGd0N3bJ3YYeG2gkxoSJjiaAg3M9FOpB1w/0?wx_fmt=jpeg';
        echo "' />";
    }
    
}
?>